package com.tk.state;
/*
    线程休眠：
        1、sleep(时间)指定当前线程阻塞的毫秒数
        2、sleep存在异常InterruptedException
        3、sleep时间达到后线程进入就绪状态
        4、sleep可以模拟网络延时，倒计时等
        5、每一个对象都有一个锁，sleep不会释放锁
 */
public class TestSleep implements Runnable {
    private int ticketNums = 10;

    @Override
    public void run() {
        while (true) {
            if (ticketNums <= 0) {
                break;
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNums-- + "张票");
        }
    }

    public static void main(String[] args) {
        TestSleep testSleep = new TestSleep();

        new Thread(testSleep,"小明");
        new Thread(testSleep,"老师");
        new Thread(testSleep,"黄牛");
    }
}
